//
//  UIButton+Extension.swift
//  AispeechMobileApp
//
//  Created by Lee on 2019/2/25.
//  Copyright © 2019年 Lee. All rights reserved.
//

import Foundation

enum ButtonEdgeInsetsStyle {
    case Top // image在上，label在下
    case Left // image在左，label在右
    case Bottom // image在下，label在上
    case Right // image在右，label在左
    
}

extension UIButton {
    
    //MARK: - 调整button图片与文字位置
    /// 调整button图片与文字位置
    ///
    /// - Parameters:
    ///   - style: 样式
    ///   - space: 距离
    func layoutButtonEdgeInsets(style:ButtonEdgeInsetsStyle, space:CGFloat) {
        
        var labelWidth : CGFloat = 0.0
        var labelHeight : CGFloat = 0.0
        var imageEdgeInset = UIEdgeInsets.zero
        var labelEdgeInset = UIEdgeInsets.zero
        let imageWith = self.imageView?.frame.size.width
        let imageHeight = self.imageView?.frame.size.height

        labelWidth = (self.titleLabel?.intrinsicContentSize.width)!
        labelHeight = (self.titleLabel?.intrinsicContentSize.height)!

        switch style {
        case .Top:
            imageEdgeInset = UIEdgeInsets(top: -labelHeight-space/2.0, left: 0, bottom: 0, right: -labelWidth)
            labelEdgeInset = UIEdgeInsets(top: 0, left: -imageWith!, bottom: -imageHeight!-space/2.0, right: 0)
        case .Left:
            imageEdgeInset = UIEdgeInsets(top: 0, left: -space/2.0, bottom: 0, right: space/2.0)
            labelEdgeInset = UIEdgeInsets(top: 0, left: space/2.0, bottom: 0, right: -space/2.0)
        case .Bottom:
            imageEdgeInset = UIEdgeInsets(top: 0, left: 0, bottom: -labelHeight-space/2.0, right: -labelWidth)
            labelEdgeInset = UIEdgeInsets(top: -imageHeight!-space/2.0, left: -imageWith!, bottom: 0, right: 0)
        case .Right:
            // To Do print("坐标是====\(labelWidth)=====\(space)")
            imageEdgeInset = UIEdgeInsets(top: 0, left: labelWidth+space/2.0, bottom: 0, right: -labelWidth-space/2.0)
            labelEdgeInset = UIEdgeInsets(top: 0, left: -imageWith!-space/2.0, bottom: 0, right: imageWith!+space/2.0)

        }
        self.titleEdgeInsets = labelEdgeInset
        self.imageEdgeInsets = imageEdgeInset
        
    }
    
    //MARK: - 开启button图片360度旋转
    func startRotate() {
        let rotationAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
        rotationAnimation.toValue = Double.pi * 2
        rotationAnimation.duration = 3
        rotationAnimation.isCumulative = true
        rotationAnimation.repeatCount = MAXFLOAT
        rotationAnimation.isRemovedOnCompletion = false
        self.imageView?.layer.add(rotationAnimation, forKey: "rotationAnimation")
    }
    
    //MARK: - 移除button图片360度旋转效果
    func stopRotate() {
        self.imageView?.layer.removeAllAnimations()
    }
    
}

